home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW noweb 2.7 / src / perl / cpif next >
Encoding:
Text File  |  1995-10-04  |  1.2 KB  |  58 lines  |  [TEXT/MPS ]

  1. Perl -Sx "{0}" {"Parameters"}; Exit
  2.  
  3. #!perl
  4. # cpif [ -eq -ne ] file...
  5. # copy standard input to each of the named files
  6. # if new * old is true or old doesn't exist;
  7. # * defaults to -ne
  8.  
  9. # set -x
  10. $op    = "ne";
  11.  
  12. if ($ARGV[0] =~ /^-(\w+)$/) {
  13.     $op = $1;
  14.     die "Usage: cpif [ -eq -ne ] file" unless $op =~ /eq|ne/;
  15.     shift;
  16.  
  17. if ($op eq "ne") {
  18.     $good = 0;
  19.     open(OLD, $ARGV[0]) || goto docopy;
  20.     for (;;) {
  21.         $chunk = sysread(STDIN, $text, 8192);
  22.         $oldlen= sysread(OLD, $old, $chunk);
  23.         if (!$chunk && !$oldlen) {
  24.             close(OLD);
  25.             last;
  26.         } elsif ($oldlen != $chunk || $text ne $old) {
  27.             close(OLD);
  28. docopy:
  29.             open(NEW, "+>$ARGV[0]") || die "Couldn't open $ARGV[0] for writing";
  30.             seek(NEW, $good, 0);
  31.             syswrite(NEW, $text, $chunk);
  32.             $good += $chunk;
  33.             while($chunk = sysread(STDIN, $text, 8192)) {
  34.                 syswrite(NEW, $text, $chunk);
  35.                 $good += $chunk;
  36.             }
  37.             truncate(NEW, $good);
  38.             close(NEW);
  39.             last;
  40.         } 
  41.         $good += $chunk;
  42.     } 
  43. } else {
  44.     for (;;) {
  45.         $chunk = sysread(STDIN, $text, 8192);
  46.         $oldlen= sysread(OLD, $old, $chunk);
  47.         if (!$chunk && !$oldlen) {
  48.             close(OLD);
  49.             $now = time;
  50.             utime $now, $now, $ARGV[0];
  51.             last;
  52.         } elsif ($oldlen != $chunk || $text ne $old) {
  53.             close(OLD);
  54.             last;
  55.         } 
  56.     }     
  57. }